package com.ywqf_api.service.impl;


import com.ywqf_api.dao.CustomerLoginDao;
import com.ywqf_api.dto.exc.ToLoginExcution;
import com.ywqf_api.entity.*;
import com.ywqf_api.enums.ToLoginEnum;
import com.ywqf_api.service.CustomerLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ywqf_api.entity.UserLogin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerLoginServiceImpl implements CustomerLoginService {
    @Autowired
    private CustomerLoginDao customerLoginDao;
    @Override
    public UserLogin loginPhone(String phoneNum) {
        UserLogin userLogin=customerLoginDao.loginPhone(phoneNum);
        return userLogin;
    }

    @Override
    public int addLogin(String telephone, String password) {
        int num=customerLoginDao.addLogin(telephone, password);
        return num;
    }

    @Override
    public int updateUser(String telephone, String password) {
        int num=customerLoginDao.updateUser(telephone,password);
        return num;
    }

    @Override
    public int updatePasswordByCustomerId(String telephone,String password,String newPassword) {
        int num=customerLoginDao.updatePasswordByCustomerId(telephone,password,newPassword);
        return num;
    }
    @Override
    public UserLogin getPassword(int customerId) {
        UserLogin userLogin = customerLoginDao.getPasswordByCustomerId(customerId);
        return userLogin;
    }

    @Override
    public int updateTelephone(String newTelephone,String telephone,String password) {
        int num=customerLoginDao.updateTelephone(newTelephone,telephone,password);
        return num;
    }

    @Override
    public UserLogin getPasswordAndTelephone(String telephone) {
        UserLogin userLogin=customerLoginDao.getPasswordAndTelephone(telephone);
        return userLogin;
    }

    @Override
    public UserAll selectAllOfTelephone(String password,String telephone) {
        UserAll userAll=customerLoginDao.selectAllOfTelephone(password,telephone);
        return userAll;
    }

    @Override
    public int selectPastTel(String newTelephone) {
        int selectPastTel=customerLoginDao.selectPastTel(newTelephone);
        return selectPastTel;
    }

    @Override
    public ToLoginExcution findUserPark(int houseId) {
        Parking park=customerLoginDao.findUserPark(houseId);
        House house=customerLoginDao.findUserHouse(houseId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("park",park);
        map.put("house",house);
        return new ToLoginExcution(ToLoginEnum.SUCCESS, map);
    }

    @Override
    public String findServiceEndTime(int houseId) {
        String findServiceEndTime=customerLoginDao.findServiceEndTime(houseId);
        if(findServiceEndTime.length() >= 19){
            findServiceEndTime = findServiceEndTime.substring(0,19);
        }
        return findServiceEndTime;
    }

    @Override
    public ToLoginExcution findAll(int houseId) {
        All estate=customerLoginDao.findAll(houseId);
        if(estate != null){
            estate.setPayDate(estate.getPayDate().substring(0,19));
            estate.setServiceStartDate(estate.getServiceStartDate().substring(0,19));
            estate.setServiceEndDate(estate.getServiceEndDate().substring(0,19));
        }
        WaterPay waterPay=customerLoginDao.findWaterPay(houseId);
        if(waterPay !=null){
            waterPay.setPayTime(waterPay.getPayTime().substring(0,19));
        }
        Parking park=customerLoginDao.findUserPark(houseId);
        ElectricityPay electricityPay=customerLoginDao.findElectricityPay(houseId);
        if(electricityPay != null){
            electricityPay.setRecordTime(electricityPay.getRecordTime().substring(0,19));
        }
        HeatingPay heatingPay=customerLoginDao.findHeatingPay(houseId);
        if(heatingPay != null){
            heatingPay.setPaymentDate(heatingPay.getPaymentDate().substring(0,19));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("waterPay",waterPay);
        map.put("estate",estate);
        map.put("park",park);
        map.put("electricityPay",electricityPay);
        map.put("heatingPay",heatingPay);
        return new ToLoginExcution(ToLoginEnum.SUCCESS, map);
    }

    @Override
    public List<HeatingPaid> heatingPayMsg(int houseId) {
        List<HeatingPaid> heatingPayMsg =customerLoginDao.heatingPayMsg(houseId);
        for(int i=0; i<heatingPayMsg.size();i++){
            HeatingPaid heatingPaid = heatingPayMsg.get(i);
            heatingPaid.setPaymentDate(heatingPaid.getPaymentDate().substring(0,19));
        }
        return heatingPayMsg;
    }

    @Override
    public List<EstatePay> housekeepingPay(int houseId) {
        List<EstatePay> housekeepingPay=customerLoginDao.housekeepingPay(houseId);
        for(int i=0;i<housekeepingPay.size();i++){
            EstatePay estatePay = housekeepingPay.get(i);
            estatePay.setPayDate(estatePay.getPayDate().substring(0,19));
        }
        return housekeepingPay;
    }

    ;






}
